package com.zbw.bleeleweight.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.zbw.bleeleweight.R;
import com.zbw.weightlibrary.Activity.BleConnectActivity;
import com.zbw.weightlibrary.Activity.SerialConnectActivity;
import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;
import com.zbw.weightlibrary.Connector.NewWeight;
import com.zbw.weightlibrary.Connector.WeightData;


/**
 * 作者 ：Wesley
 * 时间 ：2019-12-26 15:38
 * 这个类是干嘛的？：TestActivity
 */
public class TestActivity extends AppCompatActivity {
    private IBleControl mNewWeight;
    private TextView text;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        text = findViewById(R.id.text);

        findViewById(R.id.bottom1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TestActivity.this, SerialConnectActivity.class);
                intent.putExtra("key",NewWeight.WEIGHTSERIAL);
                startActivity(intent);
            }
        });

        WeightData weightData = new WeightData();
       // weightData.setAddress("20:17:09:06:58:27");
        weightData.setPort("ttysWK2");
        weightData.setBaudrate(19200);
      /*  WeightDataSave.setParam(this, "sp_serial_port", "ttyS1");
        WeightDataSave.setParam(this, "sp_serial_brate", 9600);*/

        mNewWeight =  NewWeight.getInstance().NewControl(NewWeight.Histone, TestActivity.this, new IinfoCallback() {
            @Override
            public void onConnect(String msg, boolean result) {
                Log.i("onConnect",msg);
                showText(msg);
            }

            @Override
            public void onDisconnect(String msg, boolean result) {
                Log.i("onDisconnect",msg);
                showText(msg);
            }

            @Override
            public void onGetWeight(String String, boolean isSta) {
                Log.i("onGetWeight",String+isSta);
                if(isSta){
                    showText("稳定"+String);
                }else{
                    showText("不稳定"+String);
                }
            }

        }, weightData);

        findViewById(R.id.bottom2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text.setText("连接中");
                mNewWeight.bleConnect();
            }
        });
        findViewById(R.id.bottom3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNewWeight.getWeight();
            }
        });
        findViewById(R.id.bottom4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNewWeight.stopWeight();
            }
        });

        findViewById(R.id.bottom5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNewWeight.bleDisconnect();
            }
        });

        findViewById(R.id.bottom6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TestActivity.this, BleConnectActivity.class));
            }
        });
        findViewById(R.id.bottom7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TestActivity.this, SerialConnectActivity.class));
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mNewWeight.bleDisconnect();
    }

    private void showText(final String msg){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text.setText(msg);

            }
        });
    }

}
